/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mybatisplus.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.mybatisplus.utils.HttpStatus;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Data
public class ResponseEntityT<T> implements Serializable {


	@ApiModelProperty("泛型")
	private T result;

	@ApiModelProperty("返回处理消息")
	private String message;

	@ApiModelProperty("返回代码")
	private Integer code;

	/**
	 * 成功标志
	 */
	@ApiModelProperty(value = "成功标志")
	private boolean success = true;

	public static ResponseEntityT OK() {
		return new ResponseEntityT();
	}
	public static ResponseEntityT error() {
		return new ResponseEntityT("请求失败",500 );
	}
	public static ResponseEntityT error(String message) {
		return new ResponseEntityT(message,500 );
	}
	public static ResponseEntityT AUTHZ(String message) {
		return new ResponseEntityT(message,510 );
	}
	public static ResponseEntityT AUTHZ() {
		return new ResponseEntityT("请求越权",510 );
	}
	public static ResponseEntityT OK(String message) {
		return new ResponseEntityT(message);
	}
	public static ResponseEntityT OK(Object data) {
		return new ResponseEntityT(data);
	}
	public static ResponseEntityT OK(Objects result) {
		return new ResponseEntityT(result);
	}
	public static ResponseEntityT OK(IPage<?> result) {
		return new ResponseEntityT(result);
	}
	public static ResponseEntityT OK(List<?> result) {
		return new ResponseEntityT(result);
	}
	public static ResponseEntityT OK(Objects result,String message) {
		return new ResponseEntityT(result,message);
	}

	public ResponseEntityT() {
		this.message = "请求成功";
		this.code = 200;
		this.success = true;
	}

	public ResponseEntityT(String message) {
		this.message=message;
		this.code = 200;
		this.success = true;
	}

	public ResponseEntityT(T result) {
		this.result=result;
		this.message = "请求成功";
		this.code = 200;
		this.success = true;
	}
	public ResponseEntityT(T result, String message) {
		this.result=result;
		this.message = message;
		this.code = 200;
		this.success = true;
	}

	public ResponseEntityT(String message, Integer code) {
		this.code = code;
		this.message=message;
		if(code.equals(200)){
			this.success = true;
		}else{
			this.success = false;
		}
	}

	public ResponseEntityT(T result, String message, Integer code) {
		this.result=result;
		this.code = code;
		this.message=message;
		if(code.equals(200)){
			this.success = true;
		}else{
			this.success = false;
		}
	}

	public ResponseEntityT(HttpStatus httpStatus) {
		this.code = httpStatus.getValue();
		this.message=httpStatus.getReasonPhrase();

		if(httpStatus.getValue().equals(200)){
			this.success = true;
		}else{
			this.success = false;
		}
	}

	public ResponseEntityT(String message, HttpStatus httpStatus) {
		this.code = httpStatus.getValue();
		this.message=message;
		if(httpStatus.getValue().equals(200)){
			this.success = true;
		}else{
			this.success = false;
		}
	}

	public ResponseEntityT(T result, HttpStatus httpStatus) {
		this.result=result;
		this.code = httpStatus.getValue();
		this.message=httpStatus.getReasonPhrase();
		if(httpStatus.getValue().equals(200)){
			this.success = true;
		}else{
			this.success = false;
		}
	}


}
